// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
/**
 * 排行榜
 */
let base = require("panelBase")
let viewConfig = {
    btn_close:{
        node:null,
        name:"btn_close",
        eventType:"click",
        cb:"onBtnCloseClick"
    },

    tips:{
        node:null,
        name:"tips",
    },
    item:{
        node:null,
        name:"item",
        active:false,
    },
    item_my:{
        node:null,
        name:"item_mySelf",
    },
    sc:{
        node:null,
        name:"shopSc",
    }
};


cc.Class({
    extends: base,

    properties: {
       rankList:[],
       mySc:null,
    },

    init () {
        this._super(viewConfig);
        this.onHide();
        this.updateInfo(viewConfig["tips"].node,"每小时更新一次",cc.Label);
        
        this.node.addComponent("myScrollView");
        this.mySc = this.node.getComponent("myScrollView");
        this.mySc.init(viewConfig["sc"].node);

        this.rankList = this.getRankList();

        cc.tg.eventCenter.addEvent("onRefreshRank",(data)=>{
            this.refreshRank(data);
        })
        this.isInited = true;
 
    },



    onShow(){
        this._super();
        //let mySc = this.node.getComponent("myScrollView");
        setTimeout(()=>{
            let testList = [];
            for(let i = 0 ; i < 10 ;++i)
            {
                let testData = {
                    rank:1,
                    headUrl:"",
                    nickName:"张三",
                    coin:0,
                    level:5,
                    topItemName:"分红蛋糕",
                }
                testData.rank = i+1;
                testData.nickName = i%2 == 0 ? "fans":"lisi";
                testData.coin = i*50;
                testList.push(testData);
            }

            this.mySc.showData(viewConfig["item"].node,testList,this.initItem.bind(this),10);

            this.initItem(viewConfig["item_my"].node,testList[0])
        },1000)
        cc.tg.gameLogic.toRefreshRank();

    },

    refreshRank(data){
        if(!data)
            return ;
        
    },  

    initItem(node,data){
        if(!node || !data)
            return ;
        let levelBg = node.getChildByName("rank_bg");
        this.updateInfo(node.getChildByName("head"),this.getHeadByIndex(data.rank),cc.Sprite)
        if(data.rank <= 3)
        {
            this.updateInfo(levelBg,this.rankList[data.rank-1],cc.Sprite);
        }
        else
        {
            this.updateInfo(levelBg,null,cc.Sprite);
        }
        this.updateInfo(node.getChildByName("rankLabel"),data.rank,cc.Label);
        this.updateInfo(node.getChildByName("nickName"),data.nickName,cc.Label);
        this.updateInfo(node.getChildByName("coinLabel"),data.coin,cc.Label);
        this.updateInfo(node.getChildByName("levelLabel"),data.level,cc.Label);
        this.updateInfo(node.getChildByName("topItemName"),data.topItemName,cc.Label);
    },




    onBtnCloseClick(){
        this.onHide();
    },

    


    onDestroy(){
        for(let key in viewConfig)
        {
            viewConfig[key].node = null;
        }
    },
});
